/**  
 * @author renbangjie renbangjie@126.com  
 * @date 2015-3-26 上午11:41:11
 * @version V1.0  
 * <b>Copyright (c)</b> 2014医信金融信息服务（上海）有限公司-版权所有<br/>
 */
package com.yjd.comm.pay.util;

import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.StringUtils;

import com.yjd.comm.pay.model.EbankOnlineOrder;
import com.yjd.comm.system.model.SysParamModel;
import com.yjd.comm.system.util.SysParamUtil;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.vo.Constant.SysParamKeyEnum;

/**
 * 网银在线的工具类
 * 
 * @author renbangjie renbangjie@126.com
 * @date 2015-3-26 上午11:41:11
 */
public class EbankOnlineUtils extends FrameUtil {

	/**
	 * 校验签名
	 * 
	 * @param paramMap
	 * @return boolean
	 */
	public static boolean authentication(Map<String, Object> paramMap) {
		EbankOnlineOrder ebankOrder = parseCallBackOrder(paramMap);
		if (ebankOrder == null) {
			FrameUtil.throwBaseException1("解析订单信息出错");
		}
		String signatrue = generateCallBackSignature(ebankOrder);
		if (StringUtils.isBlank(signatrue)) {
			FrameUtil.throwBaseException1("解析签名出错");
		}

		return signatrue.compareTo(ebankOrder.getBackSignature()) == 0;

	}

	/**
	 * 生成返回的签名
	 * 
	 * @param ebankOrder
	 * @return String
	 */
	public static String generateCallBackSignature(EbankOnlineOrder ebankOrder) {
		if (ebankOrder == null) {
			FrameUtil.throwBaseException1("订单信息异常");
		}
		EbankOnlineOrder config = EbankOnlineUtils.getConfig();
		return DigestUtils.md5Hex(ebankOrder.getTransID() + ebankOrder.getResult()
				+ ebankOrder.getFactMoney() + ebankOrder.getCurrencyType() + config
				.getSalt()).toUpperCase();

	}

	/**
	 * 解析返回的订单
	 * 
	 * @param paramMap
	 * @return EbankOnlineOrder
	 */
	public static EbankOnlineOrder parseCallBackOrder(
			Map<String, Object> paramMap) {
		if (paramMap == null) {
			FrameUtil.throwBaseException1("订单信息异常");
		}
		EbankOnlineOrder ebankOrder = new EbankOnlineOrder();
		ebankOrder.setTransID((String) paramMap.get("v_oid"));
		ebankOrder.setResult((String) paramMap.get("v_pstatus"));
		ebankOrder.setMessage((String) paramMap.get("v_pstring"));
		ebankOrder.setFactMoney((String) paramMap.get("v_amount"));
		ebankOrder.setCurrencyType((String) paramMap.get("v_moneytype"));
		ebankOrder.setBackSignature((String) paramMap.get("v_md5str"));
		return ebankOrder;
	}

	/**
	 * 生成订单
	 * 
	 * @param date
	 * @param orderID
	 * @param orderMoney
	 * @param bankCode
	 * @param productName
	 * @return EbankOnlineOrder
	 */
	public static EbankOnlineOrder generateOrder(Long orderID,
			Double orderMoney, String bankCode, String productName) {
		if (orderID == null || orderMoney == null || orderMoney <= 0
				|| StringUtils.isBlank(bankCode)) {
			FrameUtil.throwBaseException1("参数异常");
		}
		EbankOnlineOrder ebankOrder = EbankOnlineUtils.getConfig();
		ebankOrder.setTransID(ebankOrder.getMerchantID() + "-" + orderID);
		ebankOrder.setOrderMoney(String.valueOf(orderMoney));
		ebankOrder.setBank(bankCode);
		ebankOrder.setSendSignature(generateSignature(ebankOrder));
		ebankOrder.setRemark(productName);
		return ebankOrder;
	}

	/**
	 * 创建签名
	 * 
	 * @param ebankOrder
	 * @return String
	 */
	public static String generateSignature(EbankOnlineOrder ebankOrder) {
		if (ebankOrder == null) {
			FrameUtil.throwBaseException1("参数异常");
		}
		return DigestUtils.md5Hex(ebankOrder.getOrderMoney() + ebankOrder.getCurrencyType()
				+ ebankOrder.getTransID() + ebankOrder.getMerchantID()
				+ ebankOrder.getPageUrl() + ebankOrder.getSalt()).toUpperCase();
	}

	/**
	 * 解析订单号
	 * 
	 * @param transID
	 * @return Long
	 * @throws Exception
	 */
	public static Long getOrderID(String transID) throws Exception {
		if (StringUtils.isBlank(transID)) {
			FrameUtil.throwBaseException1("订单号异常");
		}
		String temp = EbankOnlineUtils.getConfig().getMerchantID() + "-";
		if (!transID.contains(temp)) {
			return null;
		}
		String sid = transID.replaceFirst(temp, "");
		if (StringUtils.isBlank(sid) || !NumberUtils.isNumber(sid)) {
			throw new Exception("订单号异常");
		}
		return Long.valueOf(sid);
	}
	
	/**
	 * 生成补单的订单对象
	* @param order_id
	* @return EbankOnlineOrder
	 */
	public static EbankOnlineOrder generateReceiveOrder(Long order_id) {
		if(order_id == null){
			FrameUtil.throwBaseException1("参数异常");
		}
		
		EbankOnlineOrder ebankOrder = EbankOnlineUtils.getConfig();
		String transID = EbankOnlineUtils.getConfig().getMerchantID() + order_id;
		ebankOrder.setTransID(transID);
		ebankOrder.setSendSignature(DigestUtils.md5Hex(transID + ebankOrder.getSalt()));
		
		return ebankOrder;
	}
	

	/**
	 * 获取配置信息
	 * 
	 * @return EbankOnlineOrder
	 */
	public static EbankOnlineOrder getConfig() {
		String charset = getConstResource("ebankol.charset");
		String merchantID = getConstResource("ebankol.merchantID");
		String pageUrl = getConstResource("ebankol.pageUrl");
		String returnUrl = getConstResource("ebankol.returnUrl");
		String payUrl = getConstResource("ebankol.payUrl");
		String salt = getConstResource("ebankol.salt");
		String currency = getConstResource("ebankol.currencyType");

		EbankOnlineOrder config = new EbankOnlineOrder();

		config.setSalt(salt);
		config.setMerchantID(merchantID);
		config.setCharset(charset);
		config.setPageUrl(pageUrl);
		config.setReturnUrl("[url:=" + returnUrl + "]");
		config.setPayUrl(payUrl + "?encoding=" + charset);
		config.setCurrencyType(currency);

		return config;
	}

}
